package com.p2p.front.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.base.annotation.LoginNeedless;
import com.p2p.base.page.PageVo;
import com.p2p.base.util.DateUtil;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.service.FinanceService;
import com.p2p.front.service.LoanRepayService;
import com.p2p.front.vo.FinanceVO;

@Controller
@RequestMapping("/finance")
public class FinanceController extends BaseController{
	
	@Resource
	private FinanceService financeService;
	
	@Resource
	private LoanRepayService loanRepayService;
	
	@RequestMapping("/finance")
	@LoginNeedless
	public String getFinance(Model model){
		//获取总的资金记录用于页面展示
		FinanceVO finance = financeService.getTotleFinance();
		model.addAttribute("finance", finance);
		return "/finance/finance";
	}
	
	@RequestMapping("/getLoanRepay7")
	@LoginNeedless
	public String getTenderReturn7(PageVo form,Model model) throws ParseException{
		//获取未来一周内应收回欠款数据
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 6);
		String endDate = new SimpleDateFormat("yyyy-MM-dd ").format(cal
				.getTime());
		String beginDate = new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
		long begin = DateUtil.parseDateTime(beginDate + "00:00:00").getTime();
		long end = DateUtil.parseDateTime(endDate+ " 23:59:59").getTime();
		form.getOtherData().put("begin", begin);
		form.getOtherData().put("end", end);
		PageVo loanRepay7  = loanRepayService.queryWillExpireByDaysPage(7, form);
		model.addAttribute("loanRepay7", loanRepay7);
				
		return "/finance/loanRepay7";
	}
	
	@RequestMapping("/getExceedOverRepay")
	@LoginNeedless
	public String getExceedOverRepay(PageVo form,Model model) throws ParseException{
		//获取逾期未还款数据
		String beginDate = new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
		long begin = DateUtil.parseDateTime(beginDate + "00:00:00").getTime();
		form.getOtherData().put("begin", begin);
		PageVo exceedRepay  = loanRepayService.queryHasExpireOverDaysPage(30, form);
		model.addAttribute("exceedRepay", exceedRepay);
		return "/finance/exceedRepay";
	}
	
	@RequestMapping("/getExceedLessRepay")
	@LoginNeedless
	public String getExceedLessRepay(PageVo form,Model model) throws ParseException{
		//获取逾期未还款数据
		String beginDate = new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
		long begin = DateUtil.parseDateTime(beginDate + "00:00:00").getTime();
		form.getOtherData().put("begin", begin);
		PageVo exceedRepay  = loanRepayService.queryHasExpireLessDaysPage(30, form);
		model.addAttribute("exceedRepay", exceedRepay);
		return "/finance/exceedRepay";
	}
	
	@RequestMapping("/getHasExpireAndRepayed")
	@LoginNeedless
	public String getHasExpireAndRepayed(PageVo form,Model model){
		PageVo exceedRepay  = loanRepayService.queryHasExpireAndRepayedPage(form);
		model.addAttribute("exceedRepay", exceedRepay);
		return "/finance/exceedRepayed";
	}
	
	@RequestMapping("/myFinance")
	public String myFinance(Model model){
		Integer uid=super.getCurrentUser().getIntId();
		//获取总的资金记录用于页面展示
		Map<String,Object> finance = financeService.myFinance(uid);
		model.addAttribute("finance", finance);
		return "/finance/myFinance";
	}
}
